<?php

/**
 * Created by PhpStorm.
 * User: yancheng (cheng@love.xiaoyan.me)
 * Date: 14-9-18
 * Time: 上午11:22
 */
class TemplateCore
{
    private $inited = false;
    private $twig;
    public function initEnv($path,$debug = RunMode::PRODUCT)
    {
        if($this->inited){return false;}
        include(LIB_DIR . '/libs/Twig/Autoloader.php');
        Twig_Autoloader::register();
        $loader = new Twig_Loader_Filesystem($path);
        $twig = new Twig_Environment($loader, array(
            'cache' => APP_DIR . '/cache/template',
            'debug' => $debug == RunMode::DEV
        ));
        $this->inited = true;
        $this->twig = $twig;
        return $twig;
    }

    private $vars = array();
    public function assign($key,$value){
        $this->vars[$key] = $value;
    }

    public function render($file,$vars = array()){
        $vars = array_merge($this->vars,$vars);
        echo $this->twig->render($file,$vars);
    }

}